/**
 * 创建一个请求模板
 * method：请求类型
 * url：请求的链接
 * data：请求的参数
 * onSuccess：请求成功
 * onFail：请求失败
 */
var RequestList = {}
var RequestTep = function(methods, url, data, onSuccess, onFail) {
    if (RequestList[url]) {
        // onFail("请不要重复提交");
        Tool.log("-------------url : " + url);
        Tool.log("-------------url : 请不要重复提交")
        return
    }
    RequestList[url] = true
    if(Config.channelID == 1) {
        wx.request({
            method: methods,
            url: url,
            data: data,
            header: {'content-type': 'application/json'},
            success: function (res) {
                if (res.data.code == 0) {
                    delete RequestList[url];
                    onSuccess(res.data.data);
                }else if(res.data.code == 1024) {
                    onFail("砖石不足！");
                } else if(res.data.code == 1501) {
                    onFail("抽奖失败");
                } else if(res.data.code == 1502) {
                    onFail("抽奖失败，次数不足");
                } else {
                    // var testDlg = new ErrTips("网络错误", 2000);
                    onFail("网络错误");
                }
            },
            fail: function (res) {
                onFail("网络错误");
                // var testDlg = new ErrTips("网络错误", 2000)
            },
            complete:function () {
                delete RequestList[url]
            }
        })
    }else if(Config.channelID == 200) {
        tt.request({
            method: methods,
            url: url,
            data: data,
            header: {'content-type': 'application/json'},
            success: function (res) {
                if (res.data.code == 0) {
                    delete RequestList[url];
                    onSuccess(res.data.data);
                }else if(res.data.code == 1024) {
                    onFail("砖石不足！");
                } else if(res.data.code == 1501) {
                    onFail("抽奖失败");
                } else if(res.data.code == 1502) {
                    onFail("抽奖失败，次数不足");
                } else {
                    // var testDlg = new ErrTips("网络错误", 2000);
                    onFail("网络错误");
                }
            },
            fail: function (res) {
                onFail("网络错误");
                // var testDlg = new ErrTips("网络错误", 2000)
            },
            complete:function () {
                delete RequestList[url]
            }
        })
    }else if(Config.channelID == 102) {
    
        qg.request({
            url: url,
            method: methods,
            data: data,
            dataType: 'json', // dataType可取值：string, json, arraybuffer, 表示success的回调参数对象ret的data属性是分别是：String 、Object 、ArrayBuffer类型
            success: function(res) {
                onSuccess(res);
            },
            fail: function(error, code) {
                onFail({
                    error: error,
                    code: code
                });
            }
        })
    }else {
        var xml = new XMLHttpRequest();
        xml.onreadystatechange=function() {
            if (xml.readyState === 4 && (xml.status >= 200 && xml.status < 300)) {
                Tool.log("-------init oppo success data.rand_value -------", xml.responseText);
                var xmlData = eval('(' + xml.responseText + ')');
                if (xmlData.code == 0) {
                    onSuccess(xmlData);
                }else if(xmlData.code == 1024) {
                    onFail("砖石不足！");
                } else if(xmlData.code == 1501) {
                    onFail("抽奖失败");
                } else if(xmlData.code == 1502) {
                    onFail("抽奖失败，次数不足");
                } else {
                    // var testDlg = new ErrTips("网络错误", 2000);
                    onFail("网络错误");
                }
            }else if(xml.readyState == 4 && (xml.status < 200 && xml.status >= 300)) {
                Tool.log("-------init oppo fail data.rand_value -------", xml.responseText);
                onFail("网络错误");
            }
        }
        xml.open(methods,url,true);
        xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        //xml.send(data);
        xml.send();
    }
}